package com.vinaprime.primeschedule.presentation;

import java.text.DateFormat;
import java.text.ParseException;
import java.util.Calendar;

import android.app.ActionBar;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Switch;
import android.widget.Toast;

import com.vinaprime.primeschedule.businesslayer.VPProject_BL;
import com.vinaprime.primeschedule.entities.VPProject;

public class VpprojectDetailActivity extends Activity {

	private EditText mTxtProjectCode;
	private EditText mTxtProjectName;
	private EditText mTxtProjectFromDate;
	private EditText mTxtProjectToDate;
	private EditText mTxtProjectDescription;
	private Switch mSwiProjectEnableFlag;
	private int mDateDialogId;
	static final int DATE_DIALOG_TYPE = 5;
	static final int DATE_DIALOG_ID = 0;
	private ImageButton mBtnProjectFromDate;
	private ImageButton mBtnProjectToDate;
    
	private DateFormat mDateFormat;
	private Calendar mCalendar;
    
	private VPProject mProject;
	private VPProject_BL mProjectBL;
	private ApplicationInformation mAppInfo;
    
	private String mPath, mStrIntent;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_vpproject_detail);
		ActionBar actionBar = getActionBar();
		Drawable d=getResources().getDrawable(R.raw.background_top);  
		actionBar.setBackgroundDrawable(d);
		
		mTxtProjectCode = (EditText) findViewById(R.id.txtProjectCode);
		mTxtProjectName = (EditText) findViewById(R.id.txtProjectName);
		mTxtProjectFromDate = (EditText) findViewById(R.id.txtProjectFromDate);
		mTxtProjectToDate = (EditText) findViewById(R.id.txtProjectToDate);
		mTxtProjectDescription = (EditText) findViewById(R.id.txtProjectDescription);
		mSwiProjectEnableFlag = (Switch) findViewById(R.id.swiProjectEnableFlag);
		mBtnProjectFromDate = (ImageButton) findViewById(R.id.btnProjectFromDate);
		mBtnProjectToDate = (ImageButton) findViewById(R.id.btnProjectToDate);
		
		mCalendar = Calendar.getInstance();
        
        mAppInfo = new ApplicationInformation(this);
        mPath = mAppInfo.getDBPath();
        
        mDateFormat = DateFormat.getDateInstance();
        
        mProject = new VPProject();
        mProjectBL = new VPProject_BL(this, mPath);
        
        Intent mIntent = getIntent();
		String[] strIntent = mIntent.getStringArrayExtra(VpprojectExpandableActivity.PROJECT_SENT_DETAIL);
		
		mStrIntent = strIntent[0].toString();
		
		if (mStrIntent.equals("VIEW"))
		{
			loadProjectInfo(Integer.parseInt(strIntent[1]));
			
			mTxtProjectCode.setEnabled(false);
			mTxtProjectName.setEnabled(false);
			mTxtProjectDescription.setEnabled(false);
			mSwiProjectEnableFlag.setEnabled(false);
			mBtnProjectFromDate.setEnabled(false);
			mBtnProjectToDate.setEnabled(false);
		}
		else if (mStrIntent.equals("EDIT"))
		{
			loadProjectInfo(Integer.parseInt(strIntent[1]));
		}
		else
		{
			//mMenuItem.setVisible(false);
		}
		
		mBtnProjectFromDate.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				mDateDialogId = 0;
				showDialog(DATE_DIALOG_ID);
			}
		});
		
		mBtnProjectToDate.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				mDateDialogId = 1;
				showDialog(DATE_DIALOG_ID);
			}
		});
		
		mSwiProjectEnableFlag.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				// TODO Auto-generated method stub
				if (mSwiProjectEnableFlag.isChecked())
				{
					//mSwiProjectEnableFlag.setTextOn(getResources().getText(R.string.switch_on));
					//Toast.makeText(getApplicationContext(), mSwiProjectEnableFlag.getTextOn().toString(), Toast.LENGTH_SHORT).show();
				}
				else
				{
					//mSwiProjectEnableFlag.setTextOff(getResources().getText(R.string.switch_off));
					//Toast.makeText(getApplicationContext(), mSwiProjectEnableFlag.getTextOff(), Toast.LENGTH_SHORT).show();
				}
			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.vpproject_detail, menu);
		return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.action_cancel:
			Intent mIntent = new Intent(this, VpprojectExpandableActivity.class);
			startActivity(mIntent);
			break;
		case R.id.action_save:			
			if (mStrIntent.equals("VIEW")){
				return true;
			}
			
			mProject.setVppCode(mTxtProjectCode.getText().toString());
			mProject.setVppName(mTxtProjectName.getText().toString());
			
			try {
				mProject.setVppStartDate(mDateFormat.parse(mTxtProjectFromDate.getText().toString()));
				mProject.setVppEndDate(mDateFormat.parse(mTxtProjectToDate.getText().toString()));
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			mProject.setVppDescription(mTxtProjectDescription.getText().toString());
			
			if (mSwiProjectEnableFlag.isChecked())
				mProject.setVppEnableFlag("Y");
			else
				mProject.setVppEnableFlag("N");
			
			if (mStrIntent.equals("EDIT")){
				this.mProjectBL.updateProject(this.mProject);
				Intent intent = new Intent(getApplicationContext(), VpprojectExpandableActivity.class);
				startActivity(intent);
				
				Toast.makeText(getApplicationContext(), R.string.toast_updated_message, Toast.LENGTH_SHORT).show();
			}
			else{
				this.mProject.setVppId(this.mProjectBL.getNewProjectId());
				this.mProjectBL.addProject(this.mProject);
				
				Toast.makeText(getApplicationContext(), R.string.toast_added_message, Toast.LENGTH_SHORT).show();
				
				Intent intent = new Intent(getApplicationContext(), VpprojectExpandableActivity.class);
				startActivity(intent);
			}				
			
			break;
		default:
			break;
		}
		return true;
	}
	
	@Override
    protected Dialog onCreateDialog(int id) {
        return new DatePickerDialog(this, DATE_DIALOG_TYPE,
                            mDateSetListener,
                            mCalendar.get(Calendar.YEAR), mCalendar.get(Calendar.MONTH), mCalendar.get(Calendar.DAY_OF_MONTH));
    }
	
	private DatePickerDialog.OnDateSetListener mDateSetListener =
            new DatePickerDialog.OnDateSetListener() {

                public void onDateSet(DatePicker view, int year, int monthOfYear,
                        int dayOfMonth) {
                	mCalendar.set(Calendar.YEAR, year);
                	mCalendar.set(Calendar.MONTH, monthOfYear);
                	mCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
                    updateDisplay();
                }
            };
    
    private void updateDisplay() {
        switch (mDateDialogId) {
		case 0:
			mTxtProjectFromDate.setText(this.mDateFormat.format(this.mCalendar.getTime()));
			break;
		case 1:
			mTxtProjectToDate.setText(this.mDateFormat.format(this.mCalendar.getTime()));
		default:
			break;
		}
    	
    }
    
    private void loadProjectInfo(int projectId)
    {
    	try {
			mProject = mProjectBL.getProject(projectId);
			mTxtProjectCode.setText(mProject.getVppCode());
			mTxtProjectName.setText(mProject.getVppName());
			mTxtProjectDescription.setText(mProject.getVppDescription());
			if (mProject.getVppStartDate() != null)
				mTxtProjectFromDate.setText(mDateFormat.format(mProject.getVppStartDate()));
			
			if (mProject.getVppEndDate() != null)
				mTxtProjectToDate.setText(mDateFormat.format(mProject.getVppEndDate()));
			
			if(mProject.getVppEnableFlag().equals("Y"))
				mSwiProjectEnableFlag.setChecked(true);
			else
				mSwiProjectEnableFlag.setChecked(false);
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }
}
